package com.fzshang.shopping.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Sale;
import com.fzshang.shopping.commen.bean.User;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 销售 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-12
 */
@Controller
@RequestMapping("wx/sale")
public class WXSaleController extends BaseController {

    @RequestMapping(value = "sale.html")
    public String sale(Model model, HttpServletRequest request){
        model.addAttribute("user",getWxUser(request));
        return "wx/personal/sale";
    }


    /**
     * @param sale
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdate")
    @ResponseBody
    public ApiResult saveOrUpdate(@RequestBody Sale sale){
        //根据openid查询是否是商家,是商家提示不能入驻
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid",sale.getOpenid());
        User user = userService.getOne(queryWrapper);
        if(user.getUserIdentity()==2){
            return ApiResult.fail("您已是商家,暂不能入驻销售!");
        }
        if(user.getUserIdentity()==3){
            return ApiResult.fail("您已是销售,暂不能入驻销售!");
        }
        sale.setCreateTime(System.currentTimeMillis());
        if(saleService.saveOrUpdate(sale)){
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("openid",sale.getOpenid());
            User user1 = userService.getOne(queryWrapper1);
            user1.setUserIdentity(3);
            user1.setTel(sale.getPhone());
            userService.updateById(user1);
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


}
